const db = uniCloud.database();

exports.main = async (event, context) => {
  try {
    const { id, status, reply } = event;
    
    // 参数验证
    if (!id || !status) {
      return {
        success: false,
        message: '缺少必要参数：id 或 status'
      };
    }
    
    // 验证状态值是否合法
    const validStatuses = ['pending', 'processing', 'resolved'];
    if (!validStatuses.includes(status)) {
      return {
        success: false,
        message: '不合法的状态值'
      };
    }
    
    // 准备更新数据
    const updateData = {
      status,
      updateTime: new Date().toISOString()
    };
    
    // 如果有回复内容，添加到更新数据中
    if (reply !== undefined) {
      updateData.reply = reply;
    }
    
    // 更新反馈
    await db.collection('feedbacks').doc(id).update({
      data: updateData
    });
    
    return {
      success: true,
      message: '更新成功'
    };
  } catch (error) {
    console.error('更新反馈失败:', error);
    return {
      success: false,
      message: '更新反馈失败',
      errorDetail: error.message || String(error)
    };
  }
};